package com.ctg.behavior.calc.impala.builder.event;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.common.CtCalcDate;
import com.ctg.behavior.calc.impala.report.event.CtCalcEventMeasureReport;
import com.ctg.behavior.calc.utils.CtCalcDaysUtils;
import com.ctg.behavior.common.calc.CtCalcMeasure;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.event.CtEventCalcBody;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtEventMeasureReportBuilder {

	public List<CtCalcEventMeasureReport> doBuilder(CtEventCalcBody eventCalcBody) {

		List<CtCalcEventMeasureReport> measureReports = new ArrayList<>();

		// 按请求报文中的单个Measure+时间(天)组建对象：CtCalcEventMeasureReport
		List<CtCalcMeasure> measuresList = eventCalcBody.getMeasures();
		if (measuresList.isEmpty()) {
			log.error("measures is empty, eventCalcBody={}", eventCalcBody);
			return measureReports;
		}
		String fromDate = eventCalcBody.getFromDate();
		String toDate = eventCalcBody.getToDate();
		List<CtCalcDate> reportDateList = new ArrayList<>();
		if(CtCalcConsts.UNIT_TYPE_DAY.equals(eventCalcBody.getUnit())) {
			reportDateList = CtCalcDaysUtils.listCalcDays(fromDate, toDate);
		} else if(CtCalcConsts.UNIT_TYPE_WEEK.equals(eventCalcBody.getUnit())) {
			reportDateList = CtCalcDaysUtils.listCalcWeeks(fromDate, toDate);
		} else if(CtCalcConsts.UNIT_TYPE_MONTH.equals(eventCalcBody.getUnit())) {
			reportDateList = CtCalcDaysUtils.listCalcMonths(fromDate, toDate);
		} else if(CtCalcConsts.UNIT_TYPE_HOUR.equals(eventCalcBody.getUnit())) {
			reportDateList = CtCalcDaysUtils.listCalcHours(fromDate, toDate);
		} else if(CtCalcConsts.UNIT_TYPE_MINUTE.equals(eventCalcBody.getUnit())) {
			reportDateList = CtCalcDaysUtils.listCalcMinutes(fromDate, toDate);
		} else {
			// 默认
			reportDateList = CtCalcDaysUtils.listCalcDays(fromDate, toDate);
		}
		if(reportDateList.isEmpty()){
			return measureReports;
		}
		//for (CtCalcDate theDay : reportDateList) {
		{
			int measureIndex = 0;
			for (CtCalcMeasure measure : measuresList) {
				CtCalcEventMeasureReport measureReport = new CtCalcEventMeasureReport();
				measureReport.setMeasureIndex(measureIndex);
				// 全局条件
//				measureReport.setFromDate(theDay.getFromDate());
//				measureReport.setToDate(theDay.getToDate());
				
				CtCalcDate caclDate = new CtCalcDate();
				caclDate.setFromDate(reportDateList.get(0).getFromDate());
				caclDate.setToDate(reportDateList.get(reportDateList.size()-1).getToDate());
				measureReport.setCaclDate(caclDate);
				measureReport.setCalcDateSeries(reportDateList);
				
				measureReport.setUnit(eventCalcBody.getUnit());
				
				measureReport.setByFields(eventCalcBody.getByFields());
				measureReport.setGlobalFilter(eventCalcBody.getFilter());
				// 单个measure条件
				measureReport.setEventId(measure.getEventId());
				measureReport.setName(measure.getName());
				measureReport.setField(measure.getField());
				measureReport.setAggregator(measure.getAggregator());
				measureReport.setFilter(measure.getFilter());
				// 辅助条件
				measureReport.setMaxMeasureIndex(measuresList.size()-1);
				measureReport.setMaxSeriesIndex(reportDateList.size()-1);
				
				measureReport.setAppId(eventCalcBody.getAppId());

				measureReports.add(measureReport);
				
				measureIndex++;
			}
		}
		return measureReports;
	}

}
